package com.imooc.controller;

import com.imooc.mapper.DbStuMapper;
import com.imooc.pojo.DbStu;
import com.imooc.pojo.bo.DbStuBo;
import com.imooc.service.StuService;
import com.imooc.utils.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


//@Controller
@RestController
@RequestMapping("dbstu")
@Slf4j
public class DbStuController {

    @Autowired
    private StuService stuService;

    @GetMapping("{stdId}/get")
    public String get(@PathVariable("stdId") String stdId,
                      @RequestParam Integer id,
                      @RequestParam String name) {

        log.info("stdId=" + stdId + ", id=" + id + ", name=" + name);
        return "查询Stu";
    }

    @GetMapping("/get")
    public JSONResult get(String id) throws InterruptedException {
        DbStu stu = stuService.queryById(id);

        List<DbStu> list =stuService.queryByCondition("abc",3);

        DbStu stu2 = stuService.queryByIdCustom(id);
        return JSONResult.ok(stu2);
    }


    @GetMapping("create")
    @RequestMapping("create")
    public JSONResult create(){
        String sid = UUID.randomUUID().toString();

        DbStu stu = new DbStu();

        stu.setId(sid);
        stu.setName("imooc");
        stu.setSex(1);

        stuService.saveStu(stu);
        return JSONResult.ok();
    }

    @PostMapping("create2")
    //@RequestMapping("create2")
    public JSONResult create2(@Valid @RequestBody DbStuBo stuBo,
                              BindingResult result) {
        //BindingResult是否有错误，错误信息包含在里面，如果有return
        if (result.hasErrors()) {
            Map<String, String> map = getErrors(result);
            return JSONResult.errorMap(map);
        }

        String sid = UUID.randomUUID().toString();

        DbStu stu = new DbStu();
        BeanUtils.copyProperties(stuBo,stu);

        stu.setId(sid);

        stuService.saveStu(stu);
        return JSONResult.ok();
    }

    public Map<String, String> getErrors(BindingResult result){
        Map<String, String> map = new HashMap<>();
        List<FieldError> fieldErrors = result.getFieldErrors();
        for (FieldError fieldError : fieldErrors){
            String field = fieldError.getField();
            String msg = fieldError.getDefaultMessage();
            map.put(field, msg);
        }
        return map;
    }

    //@PutMapping("update")
    @GetMapping("update")
    public JSONResult update(DbStu stu)
    {
        //DbStu stu = new DbStu();
//        stu.setId("12");
//        stu.setName("已经修改");
//        stu.setSex(22);

        stuService.updateStu(stu);
        return JSONResult.ok();
    }

    //@DeleteMapping("delete")
    @GetMapping("delete")
    public String delete() {

        DbStu stu = new DbStu();
        //主键
        //stu.setId("92a05de2-8970-4e4a-9db3-c50dafce07a2");
        //条件
        stu.setSex(2);
        //stuService.deleteStu(stu);
        //example
        stu.setName("hh");
        stuService.deleteStu(stu);
        return "删除Stu";
    }

    @GetMapping("testTrans")
    public JSONResult testTrans() {
        stuService.testTrans();
        return JSONResult.ok();
    }
}
